package cn.zhf.shop.controller;

import cn.zhf.shop.entity.Message;
import cn.zhf.shop.mapper.AccountDealMapper;
import cn.zhf.shop.service.PayService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.coyote.Request;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;


@RestController
@Api(tags = "支付模块")
@RequestMapping("api/pay")
public class PayController {
    @Resource
    PayService payService;
    @Resource
    AccountDealMapper accountDealMapper;


    @PostMapping("/common")
    @ApiOperation(value = "通用支付接口")
    public Message pay(String orderId, String orderType, int periodization, String cardId) throws UnsupportedEncodingException {
        return payService.pay(orderId, orderType, periodization, cardId);
    }

    @PostMapping("/wx")
    @ApiOperation(value = "微信支付接口")
    public Message wxpay(String orderId, String orderType) throws UnsupportedEncodingException {
        return payService.wxpay(orderId, orderType);
    }

    @PostMapping("/notify")
    @ApiOperation(value = "支付信息接收接口")
    public String notify(HttpServletRequest request ) throws UnsupportedEncodingException {

        String MerOrderNo = request.getParameter("merOrderId");
        String TranDate = request.getParameter("payTime");
        String OrderStatus = request.getParameter("status");
        String InstMid = request.getParameter("instMid");
        String AttachedData = request.getParameter("attachedData");
//        String sign = request.getParameter("sign");
//        if("".equals(Signature)){
//            System.out.println("此交易存在危险! 已中断");
//        }else{
            payService.notify(MerOrderNo.substring(4), TranDate, OrderStatus, InstMid, AttachedData);
//        }
        return "SUCCESS";
    }


    @PostMapping("/refund")
    @ApiOperation(value = "退款接口")
    public Message refund(String orderid) throws UnsupportedEncodingException {
        return payService.refund(orderid);
    }

}
